﻿using SalaryManagementSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SalaryManagementSystem.Controllers
{
    public class StafferController : Controller
    {
        private SalaryManagementBaseEntities1 db = new SalaryManagementBaseEntities1();
        // GET: Staffer
        public ActionResult Index()
        {
            if (Session["username"] == null)
            {
                return RedirectToRoute(new { controller = "Login", action = "Index" });
            }
            ViewBag.username = Session["username"];
            return View();
        }
        public ActionResult Update()
        {
            if (Session["username"] == null)
            {
                return RedirectToRoute(new { controller = "Login", action = "Index"});
            }
            return View();
        }
        [HttpPost]
        public ActionResult Update(int userid, string username,string usersex,string useraccount,string userpwd)
        {
            username = username.Trim();
            usersex = usersex.Trim();
            useraccount = useraccount.Trim();
            userpwd = userpwd.Trim();
            StafferTable st = db.StafferTable.FirstOrDefault(p => p.id == userid);
            if(username.Length == 0 || usersex.Length == 0 || useraccount.Length == 0 || userpwd.Length == 0)
            {
                ViewBag.notice = "修改不能为空!";
                return View();
            }
            if (username == Session["username"].ToString() && usersex == Session["usersex"].ToString() && useraccount == Session["useraccount"].ToString() && userpwd == Session["userpwd"].ToString())
            {
                ViewBag.notice = "请修改后再提交!";
                return View();
            }

            if (st != null)
            {
                st.account = useraccount;
                st.stafferName = username;
                st.accountpwd = userpwd;
                st.sex = usersex;
                db.Entry(st).State = System.Data.Entity.EntityState.Modified;
                if(db.SaveChanges() > 0)
                {
                    ViewBag.notice = "信息修改成功!";
                    //更新临时信息
                    Session["username"] = username;
                    Session["useraccount"] = useraccount;
                    Session["usersex"] = usersex;
                    Session["userpwd"] = userpwd;
                    ViewBag.useraccount = Session["useraccount"];
                    ViewBag.userpwd = Session["userpwd"];
                    ViewBag.username = Session["username"];
                    ViewBag.usersex = Session["usersex"];
                }
            }
            else
            {
                ViewBag.notice = "未获取到数据，请重试！";
            }
            return View();
        }
        
    }
}